/*****************************************************************************/
/* HLLib.h                                Copyright (c) Ryan Gregg 2006-2013 */
/*---------------------------------------------------------------------------*/
/* HLLib library v 2.4.5                                                     */
/*                                                                           */
/* Author : Ryan Gregg                                                       */
/* E-mail : ryansgregg@hotmail.com (Ryan Gregg)                              */
/* E-mail : m.mohr@laposte.net     (Michael Mohr)                            */
/* WWW    : http://www.nemesis.thewavelength.net/                            */
/*---------------------------------------------------------------------------*/
/*   Date  Ver      Who  Comment                                             */
/* xx.xx.xx  1.0.0  Nem  Created Original build.                             */
/* xx.xx.xx  1.0.1  Nem  Added callback functionality to                     */
/*                       CPackageUtility::Extract().                         */
/*                       Extended CGCFFile.                                  */
/*                       Minor fixes.                                        */
/* xx.xx.xx  1.0.2  Nem  Extended CPackage.                                  */
/*                       Extended CGCFFile.                                  */
/*                       Converted from bool to BOOL.                        */
/*                       HLLib now used by GCFScape.                         */
/* xx.xx.xx  1.0.3  Nem  Extended CWADFile.                                  */
/*                       Changed GetFileSizeEx() to GetFileSize() (Not all   */
/*                       versions of Windows supported the former).          */
/* xx.xx.xx  1.0.4  Nem  Fixed CPackage memory leak.                         */
/*                       Fixed CMappedPackage initialization                 */
/*                       Fixed CMappedPackage deinitialization               */
/*                       Changed CDirectoryItem::Parent to a CDirectoryFolder*/
/*                       Extended CWADFile.                                  */
/*                       Added CBSPFile which acts like CWADFile on a BSP's  */
/*                       textures.                                           */
/*---------------------------------------------------------------------------*/
/*-----------------------End of 1.0.x----------------------------------------*/
/*---------------------------------------------------------------------------*/
/* xx.xx.xx  1.1.0  Nem  Rewrote mapping code to be more flexible.           */
/*                       Packages located on disk/memory can now be loaded.  */
/*                       Optimized file mapping.                             */
/*                       Fixed CPackage::Root                                */
/*                       Fixed CGCFFile::GetFileSizeOnDisk()                 */
/* xx.xx.xx  1.1.1  Nem  Fixed CMappedPackage::Open().                       */
/* xx.xx.xx  1.1.2  Nem  Extended CPackage.                                  */
/*                       Extended CMappedPackage.                            */
/*                       Improved error messages for system calls.           */
/* xx.xx.xx  1.1.3  Nem  Extended CGCFFile.                                  */
/*                       Extended CFileMapping and CPrimitiveFileMapping     */
/*                       to allow for volatile access.                       */
/*                       Fixed CPackageUtility::Extract() illegal characters.*/
/*                       Files of size 0 B are now treated as valid files.   */
/* xx.xx.xx  1.1.4  Nem  Improved detection of corrupt packages.             */
/* xx.xx.xx  1.1.5  Nem  Improved WAD file support.                          */
/* xx.xx.xx  1.1.6  Nem  Added mipmap level option to CWADFile.              */
/* xx.xx.xx  1.1.7  Nem  Added Source engine BSP support.                    */
/* xx.xx.xx  1.1.8  Nem  Added XZP file support.                             */
/*---------------------------------------------------------------------------*/
/*-----------------------End of 1.1.x----------------------------------------*/
/*---------------------------------------------------------------------------*/
/* xx.xx.xx  2.0.0  Nem  Rewrote entire library.                             */
/*                       Open packages from anywhere (files, memory,         */
/*                       abstract streams).                                  */
/*                       Get package and item attributes.                    */
/*                       Stream package data.                                */
/*                       Validate package data.                              */
/*                       Added a C wrapper.                                  */
/*                       Improved and optimized package loading code.        */
/*                       Improved package detection.                         */
/* xx.xx.xx  2.0.1  Nem  Added memory tracking.                              */
/*                       Improved view management.                           */
/*                       Bug Fixed memory mapping.                           */
/*                       Bug Fixed user data in proc reader.                 */
/*                       Bug Fixed WAD file GetImageData().                  */
/*                       Fixed default mapping stream view size.             */
/*                       Fixed a minor memory leak.                          */
/* xx.xx.xx  2.0.2  Nem  Added GCF file defragmentation.                     */
/*                       Added fragmentation property to GCF files.          */                     
/*                       Added Dark Messiah VBSP support.                    */
/*                       Bug Fixed hlPackageCreateStream().                  */
/*                       Fixed CMapping::GetTotalMemoryUsed() calculation.   */
/*                       Fixed PAK file path parsing.                        */
/*                       Improved error messages.                            */
/*                       Improved file mapping performance.                  */
/* xx.xx.xx  2.0.3  Nem  Added NCF file support.                             */
/* xx.xx.xx  2.0.4  Nem  Bug Fixed in GCF file defragmentation progress.     */
/* xx.xx.xx  2.0.5  Nem  Added Linux support.                                */
/*                       Fixed near infinite find next recursion.            */
/* xx.xx.xx  2.0.6  Nem  Added generic write support to mapping interface.   */
/*                       Added no recurse option to FindFirst()/FindNext().  */
/*                       Improved package item attribute consistency.        */
/*                       Fixed GCC visibility support.                       */
/*                       Fixed unnecessary GCF bounds check.                 */
/*                       Fixed BSP unused lump check.                        */
/* xx.xx.xx  2.0.7  Nem  Added different search string comparisons.          */
/*                       Added Visual Studio 2005 solution.                  */
/*                       Improved package type tests.                        */
/* xx.xx.xx  2.0.8  Nem  Improved GCF version check.                         */
/*                       Fixed fragmentation header checksum after defragment*/
/* xx.xx.xx  2.0.9  Nem  Added Last Version Played attribute to GCF file.    */
/* xx.xx.xx  2.0.10 Nem  Added Last Version Played attribute to NCF file.    */
/*                       Fixed change to directory entry folder terminator.  */
/* xx.xx.xx  2.0.11 Nem  Added VBSP .lmp support.                            */
/*                       Fixed support for files over 2 GB.                  */
/*---------------------------------------------------------------------------*/
/*-----------------------End of 2.0.x----------------------------------------*/
/*---------------------------------------------------------------------------*/
/* xx.xx.xx  2.1.0  Nem  Added VPK file support.                             */
/* xx.xx.xx  2.1.1  Nem  Added support for new VPK file format.              */
/*                       Added VPK file validation.                          */
/*                       Fixed crash when closing VPK files.                 */
/* xx.xx.xx  2.1.2  Nem  Added support for new VBSP file format.             */
/*---------------------------------------------------------------------------*/
/*-----------------------End of 2.1.x----------------------------------------*/
/*---------------------------------------------------------------------------*/
/* xx.xx.xx  2.2.0  Nem  Added support for packages larger than 4 GB.        */
/* xx.xx.xx  2.3.0  Nem  Added support for x64.                              */
/*                       Added Visual Studio 2010 solution.                  */
/*                       Added Visual Studio 2008 solution..                 */
/*                       Removed Visual Studio 2003 solution.                */
/* xx.xx.xx  2.4.0  Nem  Added support for GCF files larger than 4 GB.       */
/* xx.xx.xx  2.4.1  Nem  Fixed crash in CProcStream.                         */
/*                       Fixed VPK signature check.                          */
/*                       Workaround for crash in .Net wrapper.               */
/* xx.xx.xx  2.4.2  Nem  Fixed crash when opening VBSP files with no pak file*/
/*                       lump.                                               */
/* xx.xx.xx  2.4.3  Nem  Added support for new VPK file format (v2).         */
/*                       Added SGA file support.                             */
/*                       Added ZIP deflate support.                          */
/* xx.xx.xx  2.4.4  Nem  Fixed support for VPK file format (v1).             */
/* 18.10.13  2.4.5  Nem  Added support for new SGA file format (v6).         */
/*                       Added support for new SGA file format (v7).         */
/*  Date     Ver    Who  Comment                                             */
/*xx.xx.xx 1.2.4.x  Who  Comment                                             */
/*xx:xx am               Comment                                             */
/*****************************************************************************/


#ifndef STDAFX_H
#define STDAFX_H

//-----------------------------------------------------------------------------
//Portability module for the HLLib library. Contains a wrapper symbols to make
//the compilation under Win, Lin, Mac
#include "HLLibPort.h"

#ifdef _MSC_VER
//#	ifdef HLLIB_EXPORTS
#		define HLLIB_API __declspec(dllexport)
//#	else
//#		define HLLIB_API __declspec(dllimport)
//#	endif
#else
#	if defined(HAVE_GCCVISIBILITYPATCH) || __GNUC__ >= 4
#		define HLLIB_API __attribute__ ((visibility("default")))
#	else
#		define HLLIB_API
#	endif
#endif

//-----------------------------------------------------------------------------
// Use the apropriate library
//
// The library type is encoded in the library name as the following
// HLLibXYZ.lib
// 
//  X - D for Debug version, R for Release version
//  Y - A for ANSI version, U for Unicode version
//  Z - S for static-linked CRT library, D for multithreaded DLL CRT library
//

#if defined(_MSC_VER) && !defined(__HLLIB_SELF__)
  
  #ifdef _DEBUG                                   // DEBUG VERSIONS
    #ifndef _UNICODE
      #ifdef _DLL
        #pragma comment(lib, "HLLibDAD.lib")   // Debug Ansi CRT-DLL version
      #else
		#define HLLIB_API
        #pragma comment(lib, "HLLibDAS.lib")   // Debug Ansi CRT-LIB version
      #endif
    #else
      #ifdef _DLL
        #pragma comment(lib, "HLLibDUD.lib")   // Debug Unicode CRT-DLL version
      #else
		#define HLLIB_API
        #pragma comment(lib, "HLLibDUS.lib")   // Debug Unicode CRT-LIB version
      #endif
    #endif
  #else                                           // RELEASE VERSIONS
    #ifndef _UNICODE
      #ifdef _DLL
		#pragma comment(lib, "HLLibRAD.lib")   // Release Ansi CRT-DLL version
      #else
		#define HLLIB_API
        #pragma comment(lib, "HLLibRAS.lib")   // Release Ansi CRT-LIB version
      #endif
    #else
      #ifdef _DLL
        #pragma comment(lib, "HLLibRUD.lib")   // Release Unicode CRT-DLL version
      #else
		#define HLLIB_API
        #pragma comment(lib, "HLLibRUS.lib")   // Release Unicode CRT-LIB version
      #endif
    #endif
  #endif

#endif

//-----------------------------------------------------------------------------
// Defines

typedef unsigned char		hlBool;
typedef char				hlChar;
#ifdef __cplusplus
typedef wchar_t				hlWChar;
#else
typedef unsigned short		hlWChar;
#endif
typedef unsigned char		hlByte;
typedef signed short		hlShort;
typedef unsigned short		hlUShort;
typedef signed int			hlInt;
typedef unsigned int		hlUInt;
typedef signed long			hlLong;
typedef unsigned long		hlULong;
typedef signed long long	hlLongLong;
typedef unsigned long long	hlULongLong;
typedef float				hlSingle;
typedef double				hlDouble;
typedef void				hlVoid;

typedef hlSingle		hlFloat;

#define hlFalse			0
#define hlTrue			1

#define HL_VERSION_NUMBER	((2 << 24) | (4 << 16) | (5 << 8) | 0)
#define HL_VERSION_STRING	"2.4.5"

#define HL_ID_INVALID 0xffffffff

#define HL_DEFAULT_PACKAGE_TEST_BUFFER_SIZE 8
#define HL_DEFAULT_VIEW_SIZE 131072
#define HL_DEFAULT_COPY_BUFFER_SIZE 131072

#ifdef __cplusplus
extern "C" {
#endif

typedef enum
{
	HL_VERSION = 0,
	HL_ERROR,
	HL_ERROR_SYSTEM,
	HL_ERROR_SHORT_FORMATED,
	HL_ERROR_LONG_FORMATED,
	HL_PROC_OPEN,
	HL_PROC_CLOSE,
	HL_PROC_READ,
	HL_PROC_WRITE,
	HL_PROC_SEEK,
	HL_PROC_TELL,
	HL_PROC_SIZE,
	HL_PROC_EXTRACT_ITEM_START,
	HL_PROC_EXTRACT_ITEM_END,
	HL_PROC_EXTRACT_FILE_PROGRESS,
	HL_PROC_VALIDATE_FILE_PROGRESS,
	HL_OVERWRITE_FILES,
	HL_PACKAGE_BOUND,
	HL_PACKAGE_ID,
	HL_PACKAGE_SIZE,
	HL_PACKAGE_TOTAL_ALLOCATIONS,
	HL_PACKAGE_TOTAL_MEMORY_ALLOCATED,
	HL_PACKAGE_TOTAL_MEMORY_USED,
	HL_READ_ENCRYPTED,
	HL_FORCE_DEFRAGMENT,
	HL_PROC_DEFRAGMENT_PROGRESS,
	HL_PROC_DEFRAGMENT_PROGRESS_EX,
	HL_PROC_SEEK_EX,
	HL_PROC_TELL_EX,
	HL_PROC_SIZE_EX
} HLOption;

typedef enum
{
	HL_MODE_INVALID = 0x00,
	HL_MODE_READ = 0x01,
	HL_MODE_WRITE = 0x02,
	HL_MODE_CREATE = 0x04,
	HL_MODE_VOLATILE = 0x08,
	HL_MODE_NO_FILEMAPPING = 0x10,
	HL_MODE_QUICK_FILEMAPPING = 0x20
} HLFileMode;

typedef enum
{
	HL_SEEK_BEGINNING = 0,
	HL_SEEK_CURRENT,
	HL_SEEK_END
} HLSeekMode;

typedef enum
{
	HL_ITEM_NONE = 0,
	HL_ITEM_FOLDER,
	HL_ITEM_FILE
} HLDirectoryItemType;

typedef enum
{
	HL_ORDER_ASCENDING = 0,
	HL_ORDER_DESCENDING
} HLSortOrder;

typedef enum
{
	HL_FIELD_NAME = 0,
	HL_FIELD_SIZE
} HLSortField;

typedef enum
{
	HL_FIND_MODE_WILDCARD = 0x00,
	HL_FIND_FILES = 0x01,
	HL_FIND_FOLDERS = 0x02,
	HL_FIND_ALL = HL_FIND_FILES | HL_FIND_FOLDERS,
	HL_FIND_NO_RECURSE = 0x04,
	HL_FIND_CASE_SENSITIVE = 0x08,
	HL_FIND_MODE_STRING = 0x10,
	HL_FIND_MODE_SUBSTRING = 0x20,
} HLFindType;

typedef enum
{
	HL_STREAM_NONE = 0,
	HL_STREAM_FILE,
	HL_STREAM_GCF,
	HL_STREAM_MAPPING,
	HL_STREAM_MEMORY,
	HL_STREAM_PROC,
	HL_STREAM_NULL
} HLStreamType;

typedef enum
{
	HL_MAPPING_NONE = 0,
	HL_MAPPING_FILE,
	HL_MAPPING_MEMORY,
	HL_MAPPING_STREAM
} HLMappingType;

typedef enum
{
	HL_PACKAGE_NONE = 0,
	HL_PACKAGE_BSP,
	HL_PACKAGE_GCF,
	HL_PACKAGE_PAK,
	HL_PACKAGE_VBSP,
	HL_PACKAGE_WAD,
	HL_PACKAGE_XZP,
	HL_PACKAGE_ZIP,
	HL_PACKAGE_NCF,
	HL_PACKAGE_VPK,
	HL_PACKAGE_SGA
} HLPackageType;

typedef enum
{
	HL_ATTRIBUTE_INVALID = 0,
	HL_ATTRIBUTE_BOOLEAN,
	HL_ATTRIBUTE_INTEGER,
	HL_ATTRIBUTE_UNSIGNED_INTEGER,
	HL_ATTRIBUTE_FLOAT,
	HL_ATTRIBUTE_STRING
} HLAttributeType;

typedef enum
{
	HL_BSP_PACKAGE_VERSION = 0,
	HL_BSP_PACKAGE_COUNT,
	HL_BSP_ITEM_WIDTH = 0,
	HL_BSP_ITEM_HEIGHT,
	HL_BSP_ITEM_PALETTE_ENTRIES,
	HL_BSP_ITEM_COUNT,

	HL_GCF_PACKAGE_VERSION = 0,
	HL_GCF_PACKAGE_ID,
	HL_GCF_PACKAGE_ALLOCATED_BLOCKS,
	HL_GCF_PACKAGE_USED_BLOCKS,
	HL_GCF_PACKAGE_BLOCK_LENGTH,
	HL_GCF_PACKAGE_LAST_VERSION_PLAYED,
	HL_GCF_PACKAGE_COUNT,
	HL_GCF_ITEM_ENCRYPTED = 0,
	HL_GCF_ITEM_COPY_LOCAL,
	HL_GCF_ITEM_OVERWRITE_LOCAL,
	HL_GCF_ITEM_BACKUP_LOCAL,
	HL_GCF_ITEM_FLAGS,
	HL_GCF_ITEM_FRAGMENTATION,
	HL_GCF_ITEM_COUNT,

	HL_NCF_PACKAGE_VERSION = 0,
	HL_NCF_PACKAGE_ID,
	HL_NCF_PACKAGE_LAST_VERSION_PLAYED,
	HL_NCF_PACKAGE_COUNT,
	HL_NCF_ITEM_ENCRYPTED = 0,
	HL_NCF_ITEM_COPY_LOCAL,
	HL_NCF_ITEM_OVERWRITE_LOCAL,
	HL_NCF_ITEM_BACKUP_LOCAL,
	HL_NCF_ITEM_FLAGS,
	HL_NCF_ITEM_COUNT,

	HL_PAK_PACKAGE_COUNT = 0,
	HL_PAK_ITEM_COUNT = 0,

	HL_SGA_PACKAGE_VERSION_MAJOR = 0,
	HL_SGA_PACKAGE_VERSION_MINOR,
	HL_SGA_PACKAGE_MD5_FILE,
	HL_SGA_PACKAGE_NAME,
	HL_SGA_PACKAGE_MD5_HEADER,
	HL_SGA_PACKAGE_COUNT,
	HL_SGA_ITEM_SECTION_ALIAS = 0,
	HL_SGA_ITEM_SECTION_NAME,
	HL_SGA_ITEM_MODIFIED,
	HL_SGA_ITEM_TYPE,
	HL_SGA_ITEM_CRC,
	HL_SGA_ITEM_VERIFICATION,
	HL_SGA_ITEM_COUNT,

	HL_VBSP_PACKAGE_VERSION = 0,
	HL_VBSP_PACKAGE_MAP_REVISION,
	HL_VBSP_PACKAGE_COUNT,
	HL_VBSP_ITEM_VERSION = 0,
	HL_VBSP_ITEM_FOUR_CC,
	HL_VBSP_ZIP_PACKAGE_DISK,
	HL_VBSP_ZIP_PACKAGE_COMMENT,
	HL_VBSP_ZIP_ITEM_CREATE_VERSION,
	HL_VBSP_ZIP_ITEM_EXTRACT_VERSION,
	HL_VBSP_ZIP_ITEM_FLAGS,
	HL_VBSP_ZIP_ITEM_COMPRESSION_METHOD,
	HL_VBSP_ZIP_ITEM_CRC,
	HL_VBSP_ZIP_ITEM_DISK,
	HL_VBSP_ZIP_ITEM_COMMENT,
	HL_VBSP_ITEM_COUNT,

	HL_VPK_PACKAGE_Archives = 0,
	HL_VPK_PACKAGE_Version,
	HL_VPK_PACKAGE_COUNT,
	HL_VPK_ITEM_PRELOAD_BYTES = 0,
	HL_VPK_ITEM_ARCHIVE,
	HL_VPK_ITEM_CRC,
	HL_VPK_ITEM_COUNT,

	HL_WAD_PACKAGE_VERSION = 0,
	HL_WAD_PACKAGE_COUNT,
	HL_WAD_ITEM_WIDTH = 0,
	HL_WAD_ITEM_HEIGHT,
	HL_WAD_ITEM_PALETTE_ENTRIES,
	HL_WAD_ITEM_MIPMAPS,
	HL_WAD_ITEM_COMPRESSED,
	HL_WAD_ITEM_TYPE,
	HL_WAD_ITEM_COUNT,

	HL_XZP_PACKAGE_VERSION = 0,
	HL_XZP_PACKAGE_PRELOAD_BYTES,
	HL_XZP_PACKAGE_COUNT,
	HL_XZP_ITEM_CREATED = 0,
	HL_XZP_ITEM_PRELOAD_BYTES,
	HL_XZP_ITEM_COUNT,

	HL_ZIP_PACKAGE_DISK = 0,
	HL_ZIP_PACKAGE_COMMENT,
	HL_ZIP_PACKAGE_COUNT,
	HL_ZIP_ITEM_CREATE_VERSION = 0,
	HL_ZIP_ITEM_EXTRACT_VERSION,
	HL_ZIP_ITEM_FLAGS,
	HL_ZIP_ITEM_COMPRESSION_METHOD,
	HL_ZIP_ITEM_CRC,
	HL_ZIP_ITEM_DISK,
	HL_ZIP_ITEM_COMMENT,
	HL_ZIP_ITEM_COUNT
} HLPackageAttribute;

typedef enum
{
	HL_VALIDATES_OK = 0,
	HL_VALIDATES_ASSUMED_OK,
	HL_VALIDATES_INCOMPLETE,
	HL_VALIDATES_CORRUPT,
	HL_VALIDATES_CANCELED,
	HL_VALIDATES_ERROR
} HLValidation;

typedef struct
{
	HLAttributeType eAttributeType;
	hlChar lpName[252];
	union
	{
		struct
		{
			hlBool bValue;
		} Boolean;
		struct
		{
			hlInt iValue;
		} Integer;
		struct
		{
			hlUInt uiValue;
			hlBool bHexadecimal;
		} UnsignedInteger;
		struct
		{
			hlFloat fValue;
		} Float;
		struct
		{
			hlChar lpValue[256];
		} String;
	} Value;
} HLAttribute;

typedef hlVoid HLDirectoryItem;
typedef hlVoid HLStream;

typedef hlBool (*POpenProc) (hlUInt, hlVoid *);
typedef hlVoid (*PCloseProc)(hlVoid *);
typedef hlUInt (*PReadProc)  (hlVoid *, hlUInt, hlVoid *);
typedef hlUInt (*PWriteProc)  (const hlVoid *, hlUInt, hlVoid *);
typedef hlUInt (*PSeekProc) (hlLongLong, HLSeekMode, hlVoid *);
typedef hlULongLong (*PSeekExProc) (hlLongLong, HLSeekMode, hlVoid *);
typedef hlUInt (*PTellProc) (hlVoid *);
typedef hlULongLong (*PTellExProc) (hlVoid *);
typedef hlUInt (*PSizeProc) (hlVoid *);
typedef hlULongLong (*PSizeExProc) (hlVoid *);

typedef hlVoid (*PExtractItemStartProc) (const HLDirectoryItem *pItem);
typedef hlVoid (*PExtractItemEndProc) (const HLDirectoryItem *pItem, hlBool bSuccess);
typedef hlVoid (*PExtractFileProgressProc) (const HLDirectoryItem *pFile, hlUInt uiBytesExtracted, hlUInt uiBytesTotal, hlBool *pCancel);
typedef hlVoid (*PValidateFileProgressProc) (const HLDirectoryItem *pFile, hlUInt uiBytesValidated, hlUInt uiBytesTotal, hlBool *pCancel);
typedef hlVoid (*PDefragmentProgressProc) (const HLDirectoryItem *pFile, hlUInt uiFilesDefragmented, hlUInt uiFilesTotal, hlUInt uiBytesDefragmented, hlUInt uiBytesTotal, hlBool *pCancel);
typedef hlVoid (*PDefragmentProgressExProc) (const HLDirectoryItem *pFile, hlUInt uiFilesDefragmented, hlUInt uiFilesTotal, hlULongLong uiBytesDefragmented, hlULongLong uiBytesTotal, hlBool *pCancel);

#ifdef __cplusplus
}
#endif

#include "DebugMemory.h"

#endif // STDAFX_H
